package com.ktc.controller;

import com.ktc.search.pojo.Spit;
import com.ktc.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@RestController
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    /**
     * 新增
     *
     * @param spit
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Spit spit) {
        spitService.save(spit);

        return new Result(true, StatusCode.OK, "新增吐槽成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id) {
        spitService.delete(id);

        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改
     *
     * @param spit
     * @return
     */
    @PutMapping("{id}")
    public Result update(@PathVariable String id, @RequestBody Spit spit) {
        spit.setId(id);
        spitService.update(spit);

        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Spit> spitList = spitService.findAll();

        return new Result(true, StatusCode.OK, "查询成功", spitList);
    }

    /**
     * 根据id查询
     *
     * @param
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable String id) {

        return new Result(true, StatusCode.OK, "查询成功", spitService.findById(id));
    }

    /**
     * 根据上级ID查询吐槽数据（分页）
     *
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/comment/{parentid}/{page}/{size}")
    public Result comment(@PathVariable String parentid, @PathVariable Integer page, @PathVariable Integer size) {

        Page<Spit> pageData = spitService.comment(parentid, page, size);

        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 吐槽点赞
     * 并发量高
     * 数据量高
     * 响应速度快
     * @param spitid
     * @return
     */
    @PutMapping("/thumbup/{spitid}")
    public Result thumbup(@PathVariable String spitid) {

        // 暂时写死
        String userId = "1";

        // 这个人啊，有没有点赞啊
        Object flag = redisTemplate.opsForValue().get("spit_" + spitid + "_" + userId);

        if (flag == null) {

            // 我是来点赞的啊
            spitService.thumbup(spitid);

            // 存入一个点赞标识符
            redisTemplate.opsForValue().set("spit_" + spitid + "_" + userId,1);

            return new Result(true, StatusCode.OK, "点赞成功");
        }

        // 我是来取消点赞的啊
        spitService.delThumbup(spitid);
        redisTemplate.delete("spit_" + spitid + "_" + userId);

        return new Result(true, StatusCode.OK, "取消点赞成功");
    }
}
